﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TeachInput.aspx.cs" Inherits="Credit_Teacher_ScoreInput_TeachInput" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>教师相关评估</title>
    <link href="../../Common/css/main.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="../../Common/js/jquery.js"></script>

    <script type="text/javascript" src="../../Common/js/common.js"></script>

    <script type="text/javascript" src="../../Common/js/jsUtil.js"></script>
    
     <script type="text/javascript" src="../../../../Common/js/ScoreInputUtil.js"></script>

    <script type="text/javascript" src="../../../Common/js/TableForExport.js"></script>

    <script type="text/javascript">
     var historyId = 0;
    // 加次数
        function add(obj) {
            var txt = obj.parentNode.parentNode.firstChild.firstChild;
            var val = parseFloat(txt.value);
            if (isNaN(val)) val = 0;
            txt.value = ++val;
        }
        
        // 减次数
        function minus(obj) {
            var txt = obj.parentNode.parentNode.firstChild.firstChild;
            var val = parseFloat(txt.value);
            if (isNaN(val)) val = 0;
            txt.value = val == 0 ? 0 : --val;
        }
        // 鼠标移动到计次单元格时弹出点击按钮
    	function mover(obj) {
			obj.firstChild.firstChild.firstChild.lastChild.style.display = "inline";
		}
        
        // 鼠标移出计次单元格时隐藏点击按钮
    	function mout(obj) {
			obj.firstChild.firstChild.firstChild.lastChild.style.display = "none";
		}
        
        // 保存本次结果
        function saveList() {
            var list = [];
            var trList = $("#tblHead tr");
            var stuJsonList = "";
            var jsonList = "";
            for (var i=0; i<trList.length; i++) {
                if ($(trList[i]).find("table").length == 0) continue;
                var inputList = $(trList[i]).find("input");
                var spanRemark = $(trList[i]).find("span")[0];
                var inputState = false;
                var stuList = [];
                var teacherID;
                for (var j=0; j<inputList.length; j++) {
                    var str = inputList[j].id;
                    str = str.replace("txt_", "");
                    var arr = str.split("_");
                    var moralityItemID = parseInt(arr[0]);
                    teacherID = parseInt(arr[1]);
                    
                    var val = inputList[j].value;
                    if (val != "" && isNaN(val)) {
                        showNoticMsg(str, "请填写数字");
                        return false;
                    }
                    if (val == "") continue;
                    inputState = true;
                  
                    var param = parseFloat(val);
                    var score = inputList[j].getAttribute("Score") == null ? -1 : parseFloat(inputList[j].getAttribute("Score"));
                    var operator = inputList[j].getAttribute("Math") == "" ? 0 : parseFloat(inputList[j].getAttribute("Math"));
                    var type = parseInt(inputList[j].getAttribute("MoralityType"));
                    if (type == 2 && param < 0) {
                        stuList = [];
                        //showNoticMsg(inputList[j].id, "计次题，请填写大于或等于0的次数", 0, -40);
                        showMassgeBox("提示","计次题请填写大于或等于0的次数");
                        return;
                    }
                    else if(type == 1 && score < param) {
                        //showNoticMsg(inputList[j].id, "计分题，请填写小于最大分数的的数字", 0, -40);
                        showMassgeBox("提示","计分题请填写小于最大分数的的数字");
                        return;
                    }
                    if (isNaN(operator)) operator = "0";
                    var item = "{\"MoralityItemID\": "+moralityItemID+", \"TeacherID\": "+teacherID+", \"Param\": "+param+", \"Score\": "+score+", \"Type\": "+type+", \"Operator\": "+operator+"}";
                    stuList.push(item);
                } 
                /* 填写了该学生的资料 */
                if (inputState) {
                    stuJsonList = stuList.join(",");
                    stuJsonList = "{\"TeacherID\": "+teacherID+",\"TeacherItems\":[" + stuJsonList + "]";
                    if (spanRemark != undefined && spanRemark.title != "") stuJsonList += ", \"Remark\": \"" + spanRemark.title + "\"";
                    stuJsonList += "}";
                    list.push(stuJsonList);
                }
                /* 只填了备注 */
                else {
                    if (spanRemark != undefined && spanRemark.title != "") {
                        studentID = parseInt(spanRemark.id.replace("span_",""));
                        stuJsonList = "{\"TeacherID\": "+teacherID+",\"Remark\": \"" + spanRemark.title + "\"}";
                        list.push(stuJsonList);
                    }
                }
            }
            if (stuJsonList.length > 0) {
                jsonList = list.join(",");
                jsonList = "[" + jsonList + "]";
            }
            var schoolYear = request("SchoolYear");
            if (schoolYear == "") schoolYear = -1;
            else schoolYear = parseInt(schoolYear);
            
            var term = request("Term");
            if (term == "") term = -1;
            else term = parseInt(term);
            
            var year = request("Year");
            if (year == "") year = -1;
            else year = parseInt(year);
            
            var month = request("Month");
            if (month == "") month = -1;
            else month = parseInt(month);
            
//            var clsID = request("ClsID");
//            if (clsID == "") clsID = -1;
//            else clsID = parseInt(clsID);
//            
//            var causeID = request("CauseID");
//            if (causeID == "") causeID = -1;
//            else causeID = parseInt(causeID);
            
            PageMethods.SaveScoreInput(historyId,jsonList, schoolYear, term, year, month,
                function(succ){ if (succ) showMassgeBox("保存成功","保存成功"); else showMassgeBox("保存失败","保存失败, 请检查数据是否录入或者录入的数据是否符合规则."); }, 
                function(err){return;}
            );
        }
        
        function displayRemark(obj) {
            var teacherName = obj.getAttribute("TeacherName");
            ShowPopForm("divInputRemark", "spanInputRemarkTitle", teacherName + "的备注");
            document.getElementById("hidTeacherID").value = obj.getAttribute("TeacherID");
            document.getElementById("txtRemark").value = obj.childNodes[1].title;
        }
        
        function hideRemark() {
            ClosePopForm("divInputRemark");
            document.getElementById("txtRemark").value = "";
        }
        
        function saveRemark() {
            var txtRemark = document.getElementById("txtRemark");
            if (txtRemark.value == "") {
                showNoticMsg("txtRemark", "请填写备注");
                return;
            }
            var title = txtRemark.value;
            var html = "";
            if (txtRemark.value.length > 20) {
                html = txtRemark.value.substr(0, 20) + "...";
            }
            else {
                html = txtRemark.value;
            }
            var id = "span_" + document.getElementById("hidTeacherID").value;
            var spanRemark = document.getElementById(id);
            spanRemark.innerHTML = html;
            spanRemark.title = title;
            hideRemark();
        }
        
        window.onload = function() {
            var btnReset = document.getElementById("btnReset");
            var btnSave = document.getElementById("btnSave");
            var btnExport = document.getElementById("btnExport");
            if (request("Type") == "Edit") {
                btnReset.style.display = "inline";
                btnSave.style.display = "inline";
                btnExport.style.display = "none";
            }
            else {
                btnReset.style.display = "none";
                btnSave.style.display = "none";
                btnExport.style.display = "inline";
            }
        }
        
        function reflesh() {
            window.location.href = window.document.location.href;
        }
        function CheckCh(avalue)
    {
        for   (n=0;   n<avalue.length;   n++) 
        {  
            if   (avalue[n]>128)//   中文字符
            {
                return false;
            }
        }
        return true;
    }
    function checkIsfloat(objvalue)
    {
        var value = objvalue;

        if( value.search(/^(-|\+)?\d+(\.\d+)?$/)==-1)
        {   
            if(value.length >0 && value.charAt(value.length-1)==".")
            {
                value = value.substr(0,value.length-1);
                if( value.search(/^(-|\+)?\d+(\.\d+)?$/)==-1)
                {

                    return false;
                }
                else
                {
                    if(CheckCh(value))
                    {

                        return true;
                    }
                    else
                    {

                        return false;
                    }
                    
                }
            }
            else
            {
                if(value.length == 0 ||  ( /\D/.test(value)))
                {   
   
                    return false;
                }
                else
                {

                    return true;
                }
            }
           
        }
        else
        {

            return true; 
        }

        return true;
    }
        //焦点移开后判断更新
        function optionSettingScore(obj, type) {
            var value = obj.value;
            var flag = false;
            if (type == 1) {
                // 计分题
                flag = checkIsfloat(value)
            }
            else {
                // 计次题
                flag = /^[1-9]\d*|0$/.test(value)
            }
            if(value.length == 0 ||  !flag) {   
                obj.setAttribute("value",obj.getAttribute("oldvalue")); 
            }
            else {
                obj.setAttribute("oldvalue",value); 
                settingChange = true;
            }
        }
        function getExcel() {
            var cells = GetTableInfo(document.getElementById("tblHead"));
            if(cells!=null)
            {            
                var causeID = request("CauseID");
                if (causeID == "") causeID = -1;
                else causeID = parseInt(causeID);
                
                PageMethods.GetExcel(cells,causeID,function(result){
                document.getElementById('downloadf').src = "";
                document.getElementById('downloadf').src = "../../../PostFileBytes.aspx";
                },function(err){alert(err._message);});
            }
            
        }
        
        function setTableHeight() {
//            var divTable = document.getElementById("divTable");
//            var tblHead = document.getElementById("tblHead");
//            divTable.style.height = tblHead.scrollHeight + 36;
        }
        
        function MaxLength(obj)
        {
            var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
            if (obj.getAttribute && obj.value.length>mlength)
            obj.value=obj.value.substring(0,mlength)
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <div class="mainWarp" style="width: 98%; margin: auto;padding-left:5px;">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <div style="margin-top: 5px;">
                        <h2>
                            <%= ReportTitle %>
                        </h2>
                        <br />
                        <br />
                        <%= ReportSubTitle %>
                        <input type="button" class="commonBut" id="btnReset" value="重置" onclick="reflesh();" />&nbsp;
                        <input type="button" class="commonBut" id="btnSave" value="保存" onclick="saveList();" />
                        <input type="button" class="commonBut" id="btnExport" value="导出Excel" onclick="getExcel()" style="display: none;" />
                    </div>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnReflesh" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
            <br />
             <div id="divTable" style="width:600px;  height:300px; margin:left; overflow: auto;position: relative;">
                <asp:Table ID="tblHead" runat="server" CssClass="useList" CellPadding="0" CellSpacing="0"
                    Style="border-spacing: 0px;margin-top:0px">
                </asp:Table>
            </div>
            <div id="divInputRemark" class="layoutWarp" style="display: none; width: 380px; height: 270px;">
                <div class="title">
                    <span id="spanInputRemarkTitle"></span><a alt="关闭" href="javascript:hideRemark();">关闭</a>
                </div>
                <textarea id="txtRemark" style="width: 370px; height: 200px;"   
                            onkeypress="return (/^[a-zA-Z0-9\u4e00-\u9fa5]+$/g.test(String.fromCharCode(event.keyCode)))"
                           onblur="value=value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g ,'')" 
                           onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" maxlength="50" onkeyup="return MaxLength(this)"></textarea>
                <input type="hidden" id="hidTeacherID" />
                <div style="text-align: center;">
                    <input type="button" class="commonBut" value="保存" onclick="saveRemark();" />
                    <input type="button" class="commonBut" value="取消" onclick="hideRemark();" /></div>
            </div>
        </div>
        <asp:Button ID="btnReflesh" runat="server" Style="display: none;" OnClick="btnReflesh_Click" />
        <div id="upInputPop" class="layoutWarp" style="display: none">
            <iframe src="" name="downloadf" id="downloadf" style="height: 80%;width: 95%;overflow-y:auto;overflow-x:none;" frameborder="0"   ></iframe>
        </div>
    </form>
</body>
</html>
<script language="javascript">
        var h=0;
        var w=0;
        if(document.body.scrollHeight>document.getElementById("tblHead").clientHeight)
        {
            h = document.getElementById("tblHead").clientHeight;
        }
        else
        {
            h = document.body.scrollHeight-100;
        }
        if(document.body.scrollWidth>document.getElementById("tblHead").clientWidth)
        {
            w = document.getElementById("tblHead").clientWidth+20;
        }
        else
        {
            w = document.body.scrollWidth-30;
        }
        
        if(h>0 && w>0)
        {
            document.getElementById("divTable").style.height=h+"px";
            document.getElementById("divTable").style.width=w+"px";
        }
</script>
<script>
historyId=<%=historyId %>
</script>
